package com.iot.inter.controller.newinterface.extra;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newrequest.extra.extracancel.request.RequestExtraPackagesCancel;
import com.iot.inter.entity.newrequest.extra.extracancel.response.ResponseExtraPackagesCancel;
import com.iot.inter.interfaceprocessing.newinterface.TurnToExtraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.14取消加油包:2.0接口
 * <p>
 * 接入方可以通过调用该接口，取消该订单订购的加油包。
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.14取消加油包")
public class ExtraPackagesCancelController {
    private static Logger logger = Logger.getLogger(ExtraPackagesCancelController.class);

    @CrossOrigin
    @RequestMapping(value = "extraPackageCancel", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.14取消加油包", response = ResponseExtraPackagesCancel.class)
    public String extraPackageCancel(@ApiParam @RequestBody RequestExtraPackagesCancel request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【取消加油包】请求：" + requestStr);
            RequestExtraPackagesCancel requestPackage = JSONArray.parseObject(requestStr, RequestExtraPackagesCancel.class);
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();
            String orderId = requestPackage.getOrderId();
            String refOrderId = requestPackage.getRefOrderId();


            //3.14	取消加油包
            TurnToExtraService extra = new TurnToExtraService();
            OrderPaymentManager response = extra.extraPackageCancel(orderId, refOrderId, secret, partnerCode);

            if (response != null) {

                ResponseExtraPackagesCancel res = new ResponseExtraPackagesCancel();
                res.setRetCode(response.getRetCode());
                res.setResultInfo(response.getResultInfo());
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【取消加油包】成功响应：" + jsonString);
                return jsonString;


            } else {

                ResponseExtraPackagesCancel res = new ResponseExtraPackagesCancel();
                res.setRetCode("9998");
                res.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【取消加油包】响应：" + jsonString);
                return jsonString;
            }


        } else {
            ResponseExtraPackagesCancel res = new ResponseExtraPackagesCancel();
            res.setRetCode("9998");
            res.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(res);
            logger.info("【取消加油包】响应：" + jsonString);
            return jsonString;


        }

    }
}
